package demo2;

public class MysingleList {

    class Node {
        public int val; //存储的数据
        public Node next;//存储下一个节点的地址
        public Node (int val) {
            this.val = val;
        }
    }
    public Node head; //代表当前链表的头节点的引用
    public Node creatNode (){
        Node node1 = new Node(22);
        Node node2 = new Node(22);
        Node node3 = new Node(22);
        Node node4 = new Node(42);
        Node node5 = new Node(22);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
        return this.head;
    }
    //打印链表
    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        node.next = this.head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
            return;
        }
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        //越界
        if (index < 0 || index > size()) {
            return;
        }
        //判断是不是头结点插入
        if (index == 0) {
            addFirst(data);
            return;
        }
        Node node = new Node(data);
        Node cur = this.head;
        while (--index > 0) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //判断链表是不是为空
        if (this.head == null) {
            return;
        }
        //判断头结点是不是要被删除的节点
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        Node cur = this.head;
        Node curNext = cur.next;
        while (curNext != null) {
            if (curNext.val == key) {
                cur.next = curNext.next;
                break;
            } else {
                cur = curNext;
                curNext = cur.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (this.head == null) {
            return;
        }
        Node cur = this.head;
        Node curNext = cur.next;
        while (curNext != null) {
            if (curNext.val == key) {
                cur.next = curNext.next;
            } else {
                cur = cur.next;
            }
            curNext = curNext.next;
        }
        //最后判断一下头结点是不是key
        if (this.head.val == key) {
            this.head = this.head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void clear() {
        Node cur = this.head;
        while (cur != null) {
            cur = cur.next;
            this.head.next = null;
            this.head = cur;
        }
    }
}



